<template>
  <el-container class="home-containter">
    <el-header>
      <div>后台管理系统</div>
      <el-button type="info" @click="logout" size="medium">退出</el-button>
    </el-header>

    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="200px">
        <el-menu
          background-color="#0f1216"
          text-color="#fff"
          active-text-color="rgb(83, 45, 189)"
          unique-opened
          :router="true"
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="(item, id) in menulist"
            :key="id"
          >
            <!-- 一级菜单模板区 -->
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>

            <!--  二级菜单  -->
            <el-menu-item
              :index="'/' + citem.path"
              v-for="(citem, id) in item.children"
              :key="id"
              @click="saveNavState('/' + citem.path)"
            >
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>{{ citem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主内容区 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menulist: [],
      iconObj: {
        125: "el-icon-user-solid",
        103: "el-icon-coordinate",
        101: "el-icon-goods",
        102: "el-icon-phone-outline",
        145: "el-icon-pie-chart",
      },
      activePath:''
    };
  },
  created() {
    this.getMenuList();
    this.activePath=window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status != 200) return this.$message.error(res.meta.msg);
      this.menulist = res.data;
    },
    saveNavState(activePath){
        window.sessionStorage.setItem('activePath',activePath)
        this.activePath=activePath
    }
  },
};
</script>

<style scoped>
.home-containter {
  height: 100%;
}

.el-header {
  background-color: #0f1216;
  color: cornsilk;
  text-align: center;
  line-height: 60px;
  display: flex;
  justify-content: space-between;
  padding-left: 10px;
  align-items: center;
}
.el-header > div {
  display: flex;
  align-items: center;
}

.el-aside {
  background-color: #0f1216;
  color: #333;
  /* text-align: center; */
  line-height: 200px;
}
.el-menu {
  border-right: none;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
  text-align: center;
  line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}
</style>